﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace FanaticRock.Theme.Helpers
{
    ///<summary>窗口相关的附加属性。</summary>
    public static class WindowHelper
    {
        public static bool GetFixMaxMargin8px(DependencyObject obj)
        {
            return (bool)obj.GetValue(FixMaxMargin8pxProperty);
        }

        public static void SetFixMaxMargin8px(DependencyObject obj, bool value)
        {
            obj.SetValue(FixMaxMargin8pxProperty, value);
        }

        // Using a DependencyProperty as the backing store for FixMaxMargin8px.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty FixMaxMargin8pxProperty =
            DependencyProperty.RegisterAttached("FixMaxMargin8px", typeof(bool), typeof(WindowHelper), new PropertyMetadata(false)
            {
                PropertyChangedCallback = OnFixMaxMargin8PxChanged
            });

        private static void OnFixMaxMargin8PxChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var size = (bool)e.NewValue;

            if (d is Window window)
            {
                if (size == true)
                {
                    window.SizeChanged += Window_SizeChanged;
                }
                else
                {
                    window.SizeChanged -= Window_SizeChanged;
                }
            }
        }

        private static void Window_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (sender is Window window)
            {
                //Debug.WriteLine($"WindowHelper.Window_SizeChanged: {window.WindowState}");
                if(window.Content is FrameworkElement content)
                {
                    if (window.WindowState == WindowState.Maximized)
                    {
                        content.Margin = new Thickness(8);
                    }
                    else
                    {
                        content.Margin = new Thickness(0);
                    }
                }
               
            }

        }
    }
}
